import store from '/@/store/index';
import { getModule, VuexModule, Module, Mutation, Action } from 'vuex-module-decorators';
import { useProjectSetting } from '/@/hooks/setting/index';
import { CacheTypeEnum, TOKEN_KEY, ROLES_KEY } from '/@/enums/cacheEnum';
import { getLocal, setLocal, getSession, setSession } from '/@/utils/helper/persistent';
import { RoleEnum } from '/@/enums/roleEnum';

const { permissionCacheType } = useProjectSetting();

function getCache<T>(key: string) {
  const fn = permissionCacheType === CacheTypeEnum.LOCAL ? getLocal : getSession;
  return fn(key) as T;
}

function setCache(USER_INFO_KEY: string, info: any) {
  if (!info) return;
  setLocal(USER_INFO_KEY, info, true);
  setSession(USER_INFO_KEY, info, true);
}

const NAME = 'user';
@Module({ dynamic: true, namespaced: true, store, name: NAME })
class User extends VuexModule {
  private tokenState = '';

  // 当前角色
  private roleListState: RoleEnum[] = [];

  get getTokenState(): string {
    return this.tokenState || getCache<string>(TOKEN_KEY);
  }

  get getRoleListState(): RoleEnum[] {
    return this.roleListState.length > 0 ? this.roleListState : getCache<RoleEnum[]>(ROLES_KEY);
  }

  @Action
  async login(params: { goHome?: boolean; mode?: ErrorMessageMode }) {
    try {

      const { goHome = true,mode, ...loginParams } = params
      const data = await

    } catch (err) {}
  }
}

export const userStore = getModule(User);
